<?php

if(PARCL_EXEC !== 1) {
    die('<h1>Incorrect Access</h1>You cannot access this file directly.<br/>' .
         'If you got this error message by clicking a link on the site,' .
         'please notify the webmaster as soon as possible.');
}

/**
 * Purpose     : The article module wrapper class.
 * Description : Should contain the general execution functions for the article module.
 * - Construct -
 * Not Defined.
 */

class Articles {
    public function __construct() {}

    /**
     * Input       : $id: Integer containing the ID of the article you want to retrieve.
     * Output      : $article: A properly formed array ready for template parsing.
     * Description : This function does everything necessary to properly generate
                     the article array and preparing the result-set returned to a
                     template parsable array.
     * Errors      : None.
     * Assumptions : That $id is an integer.
     * Dev Notes   : Lots of things to be done here.
     */

    public function Execute($id) {
        $article = ArticlesQueries::GetArticle($id);
        if(!is_null($article['edited'])) {
            $editedby = $this->GetEditedBy($article);
            $article['editedbyid'] = $editedby['id'];
            $article['editedby'] = $editedby['name'];
        }
        return $article;
    }

    /**
     * Input       : $article: The article array from the result array. By-reference.
     * Output      : No direct output.
     * Description : This function fetches the name of the user that edited
                     the article from the DB if it isn't the same as the authour himself.
     * Errors      : None.
     * Assumptions : That $article is an array and contains the right fields.
                     That the Registry class is available.
     * Dev Notes   : This function is needed by the news module and will be needed
                     by all modules that has an editedby field, so I was thinking
                     of making a collection of these functions and put them into a
                     class of their own.
     */

    public function GetEditedBy(&$article) {
        if($article['editedby'] != $article['authourid']) {
            $editedby = ArticlesQueries::GetEditedBy($article['editedby']);
        }
        elseif($article['editedby'] == $article['authourid']) {
            $editedby = array('id' => $article['authourid'], 'name' => $article['authour']);
        }
        return $editedby;
    }
}